<template>
	<view>
		<view>
			<image src="/static/logo_noborder.png" class='logo'></image>
		</view>
		<view>
			<view class="welcome_text">欢迎使用智社程序</view>
		</view>
		<button type="primary" @click="getUserInfo">Login</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		methods: {
			getUserInfo() {
				const _this = this
				uni.getUserProfile({
					desc: '用于完善会员资料',
					success: (result) => {
						_this.userInfo = result.userInfo
						_this.wxLogin()
					},
					fail: () => {
						uni.hideLoading();
						uni.showModal({
							content: '获取用户信息失败',
							showCancel: false
						})
					}
				})
			},
			wxLogin() {
				const _this = this
				uni.showLoading({
					title: '加载中'
				});

				uni.login({
					provider: 'weixin',
					success: (res) => { // 获取 code
						if (res.code) {
							uniCloud.callFunction({
								name: 'user',
								data: {
									action: 'code2Session',
									js_code: res.code,
									user_info: _this.userInfo
								},
								success: (res) => {
									console.log('云函数返回的值：：：：', res.result)
									uni.hideLoading();
									if (res.result.result.result._id) {
										uni.setStorageSync('userInfo', JSON.stringify(res.result
											.result.result))
										getApp().globalData.$UserInfo = res.result.result.result
									}
									uni.redirectTo({
										url:"/pages/index/index"
									})
								},
								fail: () => {
									uni.hideLoading();
									console.log('云函数调用失败')
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: white;
	}

	.logo {
		display: flex;
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-bottom: 100rpx;
		margin-left: auto;
		margin-right: auto;
	}

	.welcome_text {
		font-size: 60rpx;
		color: burlywood;
		text-align: center;
		font-family: monospace;
		font-weight: 400;
		font-style: italic;
	}

	.global_setting {
		background-color: azure;
	}

	.login_button {
		margin: 50rpx;
	}
</style>
